package com.zhike.b2b.sysparam;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.CopyUtils;
import org.apache.commons.io.FileUtils;
import org.apache.poi.util.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.zhike.b2b.company.ICompanyService;
import com.zhike.b2b.model.ParamVo;
import com.zhike.b2b.model.RoleVo;
import com.zhike.b2b.role.IRoleService;

@Controller
public class ParamAction
{
	 @Resource
	 private IParamService ps;
	 @Resource
	 private IRoleService rs;
	 @Resource
	 private ICompanyService cs;
	 
	 @RequestMapping("/admin/updateparam.do")
	 public ModelAndView updateParams(@RequestParam("param_name") String paramNames,@RequestParam("param_value") String paramValues)
	 {
		 ps.updateParams(paramNames, paramValues);
		 ModelAndView mav = new ModelAndView("/dialog/success");
		 mav.addObject("msg","参数更新成功");
		 mav.addObject("url","/admin/getparams.do");
		 return mav;
	  }
	 
	 @RequestMapping("/getparamvalue.do")
	 public @ResponseBody String getParamValue(@RequestParam("param_name") String paramName)
	 {
		  String paramValue = ps.getParamValue(paramName);
		  if(paramValue.indexOf("@")==-1&&paramValue.indexOf(".c")>-1&&paramValue.indexOf("©")==-1){
				if(paramValue.indexOf("http://")==-1){
					paramValue = "http://"+paramValue;
			}}
		  return paramValue; 
	  }
	 
	 @RequestMapping("/admin/getparams.do")
	 public ModelAndView getAllParams()
	 {
		 ModelAndView mav = new ModelAndView("/admin/params/param_info");
		 List<ParamVo> lpv = ps.getAllParams();
		 Map<String,String> params = new HashMap<String,String>();
		 for(ParamVo pv : lpv)
		 {
			 params.put(pv.getParamName(), pv.getParamValue());
		 }
		 List<RoleVo> lrv = rs.getAllRoles();
		 mav.addObject("cls", cs.getCompanyLevels("", "1"));
		 mav.addObject("params",params);
		 mav.addObject("lrv",lrv);
		 return mav; 
	  }
	 
	 @RequestMapping("/admin/updateimg.do")
	 public String editLogo(@RequestParam("param_file") MultipartFile file,@RequestParam("param_name") String paramName, 
			 HttpServletRequest request, Model model) throws IOException {
	        final String COMINPUT="upload";//设置文件夹名为companyInput的文件夹存放上传的logo图片
			String uploadFolder = request.getSession().getServletContext().getRealPath("/")+COMINPUT;
			String strUploadedfile="";
			if(file != null ){
			    String fileName = file.getOriginalFilename();	
			    long l = System.currentTimeMillis();//获取系统时间
			    String suffix = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
			    fileName = l+suffix;//把文件名设置为：系统时间.后缀名		
				if(suffix.equalsIgnoreCase(".jpg") || suffix.equalsIgnoreCase(".gif") || suffix.equalsIgnoreCase(".png") || suffix.equalsIgnoreCase(".bmp")){
				      File dirs = new File(uploadFolder+"/companyLogo");
				      if(!dirs.exists()){
				   		dirs.mkdirs();  //在不存在的目录中创建文件夹
					  }
					  
					  File uploadedFile = new File(dirs,fileName);
					  strUploadedfile= String.valueOf(uploadedFile);
					  
		    	      int rootInx = strUploadedfile.indexOf("upload");	
		  			  
					  strUploadedfile=strUploadedfile.substring(rootInx-1,strUploadedfile.length());
					  IOUtils.copy(file.getInputStream(), new FileOutputStream(uploadedFile));
					  strUploadedfile = strUploadedfile.replace("\\", "/");
					  ps.updateParam(paramName, strUploadedfile);
					  model.addAttribute("msg","参数更新成功");
				}else{
					model.addAttribute("msg","你选择的图片类型不正确，不能上传！");
				}    
			}
			
			model.addAttribute("url","/admin/getparams.do");
			return "/dialog/success";
	 }
}
